/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.game;

import com.core.utils.Utils;
import com.server.main.ClientManager;
import java.util.Random;

/**
 *
 * @author Hung
 */
public class XengGame {
    /*
     * Type
     */

    private final static int TYPE_APPLE = 0;
    private final static int TYPE_ORANGE = 1;
    private final static int TYPE_GREEN_BALL = 2;
    private final static int TYPE_BELL = 3;
    private final static int TYPE_WATER_MELLON = 4;
    private final static int TYPE_STAR = 5;
    private final static int TYPE_7 = 6;
    private final static int TYPE_BAR = 7;
    private final static int TYPE_MORE_TIME = 8;
    private final static int[] bonusMultiItem = {5, 10, 15, 20, 20, 30, 40, 100};
    /*
     * Kind
     */
    private final static int KIND_SMALL = 0;
    private final static int KIND_BIG = 1;
    /*
     * Led
     */
    private final static int TYPE_RED_LED = 1;
    private final static int TYPE_GREEN_LED = 0;
    private int typeItemFinal, kindItemFinal, typeLed, typeLedMax;
    private long totalBonus, totalMinus;
    private int[] itemCredits = new int[8], leds = new int[2], tempData;
    private ClientManager client;
    private Random random = new Random();

    public XengGame(ClientManager client) {
        this.client = client;
        resetGame();
    }

    public void resetGame() {
        totalBonus = 0;
        totalMinus = 0;

        for (int i = 0; i < 8; i++) {
            itemCredits[i] = 0;
        }

        for (int i = 0; i < 2; i++) {
            leds[i] = 0;
        }
    }

    public void processMessage(String request) {
        String[] part = Utils.split(request, '|');

        if (part[0].equals("XENG")) {
            resetGame();
            tempData = new int[part.length - 1];
            for (int i = 0; i < tempData.length; i++) {
                tempData[i] = Integer.parseInt(part[i + 1]);
            }
            processData(tempData);
        }
    }

    private void processData(int[] data) {
        for (int i = 0; i < 8; i++) {
            itemCredits[i] = data[i];
        }

        leds[0] = data[8];
        leds[1] = data[9];

        typeLedMax = leds[0] > leds[1] ? 0 : 1;

        int countLoop = 0;
        do {
            countLoop++;
            processRandom();
            if (countLoop >= 2 && typeItemFinal == TYPE_MORE_TIME) {
                continue;
            }
            client.sendMessage("RA|" + typeLed + "|" + typeItemFinal + "|" + kindItemFinal);
        } while (typeItemFinal == TYPE_MORE_TIME);
        calResult();
//        client.log(client.getUserName() + " before update :" + client.getUserMoney());
        client.updateMoneyDelta(totalBonus - totalMinus);
//        client.log(client.getUserName() + " after update :" + client.getUserMoney());
        client.sendMessage("CM|" + totalBonus);
    }

    private void processRandom() {
        random = new Random();
        int t = random.nextInt(100);
        if (t <= 5) {
            typeItemFinal = TYPE_APPLE;
            kindItemFinal = KIND_BIG;
        } else if (t <= 10) {
            typeItemFinal = TYPE_ORANGE;
            kindItemFinal = KIND_BIG;
        } else if (t == 11) {
            int t1 = random.nextInt(4);
            if (t1 == 0) {
                typeItemFinal = TYPE_BAR;
                kindItemFinal = KIND_SMALL;
            } else if (t1 == 1) {
                typeItemFinal = TYPE_MORE_TIME;
                kindItemFinal = KIND_SMALL; //Trái
            } else if (t1 == 2) {
                typeItemFinal = TYPE_MORE_TIME;
                kindItemFinal = KIND_BIG; //Phải
            } else if (t1 == 3) {
                typeItemFinal = TYPE_BAR;
                kindItemFinal = KIND_BIG; // Bar to, ra đổi tiền thôi >.<
            }
        } else if (t <= 31) {
            typeItemFinal = TYPE_APPLE;
            kindItemFinal = KIND_SMALL;
        } else if (t <= 51) {
            typeItemFinal = TYPE_ORANGE;
            kindItemFinal = KIND_SMALL;
        } else if (t <= 61) {
            typeItemFinal = TYPE_GREEN_BALL;
            kindItemFinal = KIND_SMALL;
        } else if (t <= 71) {
            typeItemFinal = TYPE_BELL;
            kindItemFinal = KIND_SMALL;
        } else if (t <= 81) {
            typeItemFinal = TYPE_WATER_MELLON;
            kindItemFinal = KIND_SMALL;
        } else if (t <= 85) {
            typeItemFinal = TYPE_STAR;
            kindItemFinal = KIND_SMALL;
        } else if (t <= 89) {
            typeItemFinal = TYPE_7;
            kindItemFinal = KIND_SMALL;
        } else if (t <= 91) {
            typeItemFinal = TYPE_7;
            kindItemFinal = KIND_BIG;
        } else if (t <= 93) {
            typeItemFinal = TYPE_STAR;
            kindItemFinal = KIND_BIG;
        } else if (t <= 95) {
            typeItemFinal = TYPE_WATER_MELLON;
            kindItemFinal = KIND_BIG;
        } else if (t <= 97) {
            typeItemFinal = TYPE_BELL;
            kindItemFinal = KIND_BIG;
        } else if (t <= 99) {
            typeItemFinal = TYPE_GREEN_BALL;
            kindItemFinal = KIND_BIG;
        }

        int r = random.nextInt(100);
        if (r <= 30) {
            typeLed = typeLedMax;
        } else {
            typeLed = 1 - typeLedMax;
        }
//        typeItemFinal = TYPE_MORE_TIME;
//        typeLed = random.nextInt(2);
    }

    private void calResult() {
        totalBonus += leds[typeLed] * 2;
        if (kindItemFinal == KIND_SMALL) {
            if (typeItemFinal != TYPE_BAR) {
                totalBonus += itemCredits[typeItemFinal] * 2;
            } else {
                totalBonus += itemCredits[typeItemFinal] * 50;
            }
        } else {
            totalBonus += itemCredits[typeItemFinal] * bonusMultiItem[typeItemFinal];
        }
        for (int i = 0; i < 8; i++) {
            totalMinus += itemCredits[i];
        }
        totalMinus += leds[0] + leds[1];
    }
}
